package com.lsk.mytuan.entity;

import com.lsk.mytuan.util.Storageable;
import com.lsk.mytuan.util.Storager;

public class Item extends Storageable {
    private int id;
    private String name;
    private float price;
    private int sales;
    private int storeId;

    private static int idCounter = 0;
    private static Storager items = new Storager();

    public Item (String name, float price, int storeId) {
        this.name = name;
        this.price = price;
        this.storeId = storeId;
        this.id = ++idCounter;
        this.sales = 0;
    }

    @Override
    public String toString() {
        String result = "";
        result += this.id;
        result += "     ";
        result += this.name;
        result += "     ";
        result += this.price;
        result += "     ";
        result += this.sales;
        return result;
    }

    @Override
    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public void setName (String name) {
        this.name = name;
    }
    public int getStoreId () {
        return this.storeId;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
    public void add() {
        Item.items.put(this);
    }

    public void increaseSales (int inc) {
        this.sales += inc;
    }

    public static Item get(int id) {
        return (Item) items.get(id);
    }

    public static int findItems (String name) {
        int count = 0;
        for (int i = 1; i <= items.length(); i++) {
            Item item = (Item) items.get(i);
            if (item == null) {
                continue;
            }
            if (item.getName().equals(name)) {
                System.out.println(item);
                count ++;
            }
        }
        return count;
    }
    
    public static boolean delete (int id) {
        return items.delete(id);
    }
}
